package sys

import (
	"database/sql"
	"encoding/json"

	"git.oschina.net/Ljohn/softCup/lib"
	_ "github.com/bmizerany/pq"
	"git.oschina.net/Ljohn/softCup/conf"
	"errors"
)


func GetUserInfoByEmail(email string) ([]byte,error) {
	db, err := sql.Open("postgres", "host="+ conf.PgsqlHost()+" password="+conf.PgsqlPwd() +" user="+conf.PgsqlUser() +" dbname="+conf.PgsqlDatabase()+" sslmode=disable")
	if err != nil {
		return nil,errors.New("连接数据库失败")
	}
	defer db.Close()
	strSQL := "SELECT authentication_token,id,email,username FROM users where email=$1"
	rows, err := db.Query(strSQL, email)
	if err != nil {
		return nil,errors.New("查询失败")
	}
	defer rows.Close()
	res, _ := lib.GetKeyValuesOne(rows)
	temp, err := json.Marshal(res)
	if err != nil {
		return nil,errors.New("JSON对象失败")
	}
	return temp,nil
}
